//
//  NewfeatureCollectionViewController.swift
//  swft_weibo
//
//  Created by mac on 15/9/14.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

let pageCount = 4
class NewfeatureCollectionViewController: UICollectionViewController {
    
    
    init(){
        
        //1.初始化布局
        let layout : UICollectionViewFlowLayout = UICollectionViewFlowLayout()
        let size = UIScreen.mainScreen().bounds.size
        layout.itemSize = size
        layout.scrollDirection=UICollectionViewScrollDirection.Horizontal
        layout.minimumInteritemSpacing=0
        layout.minimumLineSpacing=0
       
        super.init(collectionViewLayout: layout)
        
        //2.注册对应换cell,以及设置collectionview的一些属性值
        collectionView?.registerClass(NewfeatureCollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        collectionView?.bounces=false
        collectionView?.pagingEnabled=true
        collectionView?.showsHorizontalScrollIndicator=false

    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor=UIColor.redColor()
        
        //加载数据从plist中
        
        
    }

//MARK: - collectionView代理或者数据源方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let collectionCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! NewfeatureCollectionViewCell
        
        collectionCell.backgroundColor=UIColor.randomColor()
        
        collectionCell.imageURL="new_feature_"+"\(indexPath.row+1)"
        //只显示最后一个显示页中的login按钮
        indexPath.item==pageCount-1 ?(collectionCell.loginBtn?.hidden=false):(collectionCell.loginBtn?.hidden=true)
        return collectionCell
        
    }
    /**
    一个cell完全显示的时候会调用这个代理方法
    
    :param: collectionView <#collectionView description#>
    :param: cell           <#cell description#>
    :param: indexPath      <#indexPath description#>
    */
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //注意了，这个方法传给我们的indexpath返回的是上一个cell的，不是这次的cell,cell也是上一个的
//        print(indexPath)
        
        //注意下面这个方法的使用，拿到所有的显示出来的cell的indexpath
        let currentIndex = collectionView.indexPathsForVisibleItems().last
        let path = collectionView.indexPathsForVisibleItems().last?.item
        if path==pageCount-1{
        
            //开始按钮的动画
            let showCell = collectionView.cellForItemAtIndexPath(currentIndex!)
            (showCell as! NewfeatureCollectionViewCell).startLoginAnimation()
        }
        
    }

}
//MARK: - NewfeatureCollectionViewCell建立新类
//重写继承自的UICollectionViewCell
class NewfeatureCollectionViewCell : UICollectionViewCell {
    
    var showImageView : UIImageView?
    var imageURL : String?{
    
        didSet{
        
            showImageView?.image=UIImage(named: imageURL!)
        }
    
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        //1.设置子控件
        contentView.addSubview(imageView!)
        contentView.addSubview(loginBtn!)
        showImageView=imageView
        //2.布局子控件
        imageView!.xmg_Fill(contentView)
        
        loginBtn?.xmg_AlignVertical(type: XMG_AlignType.BottomCenter, referView: contentView, size: nil, offset: CGPoint(x: 0, y: -200))
        
    }
    
    private func startLoginAnimation(){
        
        loginBtn?.transform=CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(2, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 0.5, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
            
                loginBtn?.transform=CGAffineTransformIdentity
            
            }) { (_) -> Void in
                //发送通知，跳转windows的根控制器的显示页
                
                NSNotificationCenter.defaultCenter().postNotificationName("convertRootCtroller", object: false)
        }
    }
    
    
    //设置背景图片显示
    lazy var imageView : UIImageView?={
    
        let bgView = UIImageView()
        
        return bgView
    
    }()
    //设置登入按钮
    lazy var loginBtn : UIButton?={
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: UIControlState.Normal)
        
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState: UIControlState.Highlighted)
        //这里是一个注意点，当我们改变图层的锚地的时候是会更改他的布局显示的效果的，和transform形变的效果的
        btn.layer.anchorPoint=CGPointMake(0.5, 0.5)
        btn.hidden=true
        return btn
    
    }()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}




